package han.study.dao.junit;

import han.study.bean.Customer;
import han.study.dao.CustomerDaoImpl2;
import han.study.utils.JDBCUtils;
import org.junit.Test;

import java.sql.Connection;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

public class CustomerDaoImplTest2 {
    CustomerDaoImpl2 dao = new CustomerDaoImpl2();
    @Test
    public void insert() {
        Connection conn = null;
        try{
            conn = JDBCUtils.getConnection();
            String timeStr = "2015-11-11";
            java.util.Date date = new java.util.Date();
            SimpleDateFormat simpleDateFormat =(SimpleDateFormat) DateFormat.getDateInstance();
            simpleDateFormat.applyPattern("yyyy-MM-dd");
            date = simpleDateFormat.parse(timeStr);
            java.sql.Date birth =new java.sql.Date(date.getTime());
            Customer customer = new Customer(1,"于小飞","yuxiaofei@163.com",birth);
            dao.insert(conn,customer);
            System.out.println("添加成功");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(conn,null);
        }
    }

    @Test
    public void deleteById() {
        Connection conn = null;
        try{
            conn = JDBCUtils.getConnection();
            dao.deleteById(conn,20);
            System.out.println("删除成功");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(conn,null);
        }
    }

    @Test
    public void updateById() {
        Connection conn = null;
        try{
            conn = JDBCUtils.getConnection();
            Customer customer = new Customer(20,"于小飞","yuxiaofei@163.com","2015-11-12");
            dao.updateById(conn,21,customer);
            System.out.println("修改成功");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(conn,null);
        }
    }

    @Test
    public void getCustomerById() {
        Connection conn = null;
        try{
            conn = JDBCUtils.getConnection();
            Customer customer = dao.getCustomerById(conn,21);
            System.out.println(customer);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(conn,null);
        }
    }

    @Test
    public void getAll() {
        Connection conn = null;
        try{
            conn = JDBCUtils.getConnection();
            List<Customer> list = dao.getAll(conn);
            for (Customer customer:list) {
                System.out.println(customer);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(conn,null);
        }
    }
}
